<?php
class News extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->load->model('news_model');
	}
	function index($id){
		
		
		$data['news']=$this->news_model->get_news();
		$this->load->view('news_index',$data);
	}
	function view($slug=null){
		$data['news']=$this->news_model->get_news($slug);
		$this->load->view('news_item',$data);
	}
	
	function add(){
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->form_validation->set_rules('title',"Title",'required');
		$this->form_validation->set_rules('text',"Text",'required');
		
		if($this->form_validation->run()===FALSE){
			$this->load->view('news_add');
		}else{
			$this->news_model->add_news();
			redirect('news/index');
		}
	}
	function edit($id=0){
		if($id>0){
			$this->load->helper('form');
			$this->load->library('form_validation');
			$this->form_validation->set_rules('title',"Title",'required');
			$this->form_validation->set_rules('text',"Text",'required');
		
	
			if($this->input->post('update')=='update'){
				if($this->form_validation->run()===FALSE){
						echo 'failed';
				}else{
						if($this->news_model->edit_news()==TRUE){
							
							echo 'success';
						}else{
							echo 'failed';
						}
						
				}
			}
			$data['news']=$this->news_model->get_news($id);
			$this->load->view('news_edit',$data);
		}else{
			echo 'invalid';
		}
	}

	function delete($id=0){
		if($id>0){
			$this->news_model->delete_news($id);
		}else{
			echo 'invalid';
		}
	}
}
